#include "tcpserver.h"
#include "ui_tcpserver.h"
#include "mytcpserver.h"

TcpServer::TcpServer(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::TcpServer)
{
    ui->setupUi(this);
    loadConfig();
    MyTcpServer::getInstance().listen(QHostAddress(m_strIP), m_usPort);
}

TcpServer::~TcpServer()
{
    delete ui;
}

void TcpServer::loadConfig()
{
    //定义文件名
    QFile file(":/server.config");
    //打开文件（只读方式）
    if(file.open(QIODevice::ReadOnly)) {
        //读取字符串
        QByteArray baData = file.readAll();
        //转换成字符串
        QString strData = baData.toStdString().c_str();
        file.close();
        //可以替换使用空格分割，也可以直接使用\r\n分割
        //strData.replace("\r\n"," ");
        QStringList strList = strData.split("\r\n");
        //读取分割的数据
        m_strIP = strList.at(0);
        m_usPort = strList.at(1).toUShort();
        qDebug()<<"ip:"<<m_strIP<<"port:"<<m_usPort;
    }else{
        QMessageBox::critical(this, "open config", "open config failed");
    }
}
